﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.Serialization.Json;

public partial class register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string domain = URL.getDomain(Request.Url.ToString());
        string domainSuf = URL.getDomainSuf(domain);
        if (domainSuf == "")
        {
            domainSuf = "www";
        }
        Response.Write(Request.Url.ToString());
        string valueIn = Server.UrlDecode(Request.Form.ToString());
        valueIn = valueIn.Replace("'", "\"");
        XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(valueIn), XmlDictionaryReaderQuotas.Max);
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
        XElement tmproot = XElement.Parse(doc.OuterXml);
        string newDir = MapPath("") + @"\userdata\" + tmproot.Element("data").Element("username").Value +@"\";
        if (Directory.Exists(newDir))
        {
            Response.Write("2");//此用户已经被注册,不能再次注册.
            return;
        }
        switch (tmproot.Element("action").Value)
        {
            case "0":// The action is to verify the username.
                if (Directory.Exists(newDir))
                {
                    Response.Write("2"); //"2" stands for "the new username is not availbel.
                }
                else
                {
                    Response.Write("4"); // “4”表示可用
                }
                break;
            case "1":
                Directory.CreateDirectory(newDir);
                string currentDir = MapPath("") + @"\userdata\" + domainSuf + @"\";
                if (!Directory.Exists(currentDir))
                {
                    currentDir = MapPath("") + @"\userdata\www\";
                }
                CopyFolder(currentDir, newDir);
                user userInstance = new user(currentDir + @"user.xml");
                string tmp = GlobalDataHandler.getMaxID();
                userInstance.ID = Convert.ToInt64(tmp) + 1;//need to be improved
                userInstance.Us = tmproot.Element("data").Element("username").Value;
                userInstance.Em = tmproot.Element("data").Element("email").Value;
                userInstance.Ps = tmproot.Element("data").Element("password").Value;
                userInstance.setAllValue();
                userInstance.saveToFile(newDir + @"user.xml");
                Response.Write("3");
                GlobalDataHandler.addItem(userInstance.Us);//update "regTable".hbfwgcw6so
                break;
            default:
                break;
        }
    }

    private void CopyFolder(string from, string to)
    {
        if (!Directory.Exists(to))
            Directory.CreateDirectory(to);        // 子文件夹        
        foreach (string sub in Directory.GetDirectories(from))
            CopyFolder(sub + "\\", to + Path.GetFileName(sub) + "\\");
        // 文件
        foreach (string file in Directory.GetFiles(from))
            File.Copy(file, to + Path.GetFileName(file), true);
    }
}